package cn.lliiooll.ippzonetoolbox.utils;

import cn.lliiooll.ippzonetoolbox.modules.ip.IpResultData;
import cn.lliiooll.ippzonetoolbox.modules.nowm.PostData;
import cn.lliiooll.ippzonetoolbox.modules.nowm.ResultData;
import com.google.common.base.Strings;
import com.google.gson.GsonBuilder;
import lombok.SneakyThrows;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

import java.util.Arrays;
import java.util.Objects;

public class IppzoneUtils {

    public static final String ippzone_post = "http://share.ippzone.com/ppapi/share/fetch_content";

    /**
     * 通过分享帖子地址获取无水印视频
     *
     * @param url 帖子地址，例：http://share.ippzone.com/pp/post/339320410082
     * @return 无水印视频地址，例：http://tbvideo.ixiaochuan.cn/zyvd/1b/73/7d72-cf22-11ea-824b-00163e020689
     */
    @SneakyThrows
    public static String noWmVideo(String url) {
        ResultData r = getIppzoneData(url);
        return r != null ? r.getData() != null ? r.getData().getPost().getVideos().get(Arrays.asList(r.getData().getPost().getImgs()).get(0).getId() + "").getUrl() : "" : "";
    }

    public static boolean isDigit(String str) {
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    @SneakyThrows
    public static IpResultData getIpData(String ip) {
        //http://ip-api.com/json/111.113.212.73?lang=zh-CN
        // 发送请求并且取返回值
        return new GsonBuilder().serializeNulls()
                .create()
                .fromJson(new OkHttpClient().newCall(new Request.Builder()
                        .url("http://ip-api.com/json/" + ip + "?lang=zh-CN")
                        .build())
                        .execute()
                        .body()
                        .string(), IpResultData.class);
    }

    @SneakyThrows
    public static ResultData getIppzoneData(String url) {
        //http://ip-api.com/json/111.113.212.73?lang=zh-CN
        // 发送请求并且取返回值
        // 构建要提交的json
        String pid_str = url.replace("http://share.ippzone.com/pp/post/", "").replace("https://share.ippzone.com/pp/post/", "");
        //System.out.println(pid_str);
        if (!IppzoneUtils.isDigit(pid_str) && Strings.isNullOrEmpty(pid_str)) {
            return null;
        }
        return new GsonBuilder().serializeNulls()
                .create()
                .fromJson(new OkHttpClient()
                        .newCall(new Request.Builder()
                                .url(IppzoneUtils.ippzone_post)
                                .post(RequestBody
                                        .create(new GsonBuilder().serializeNulls()
                                                .create()
                                                .toJson(new PostData(Long.parseLong(pid_str))), MediaType.get("application/json; charset=utf-8")))
                                .build())
                        .execute()
                        .body()
                        .string(), ResultData.class);
    }
}